package exam.bank.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import exam.bank.dao.BankDao;
import exam.bank.entity.BankEntity;
import exam.utils.JDBCUtil;

/**
*@author jzh
*@创建时间 2017年6月17日上午10:00:24
*类说明：
*/
public class BankDaoImpl implements BankDao {

	@Override
	public int addBank(BankEntity entity) throws Exception{
		
		Connection connection = JDBCUtil.getConnection();
		String sql="insert into bank_t (bank_name,bank_phone,bank_address) values (?,?,?)";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, entity.getBankName());
		ps.setString(2, entity.getBankPhone());
		ps.setString(3, entity.getBankAddress());
		int counter=0;
		counter = ps.executeUpdate();
		return counter;
	}

	@Override
	public List<BankEntity> findAllBank() throws Exception{
		List<BankEntity> bankLists=new ArrayList<BankEntity>();
		Connection connection = JDBCUtil.getConnection();
		String sql="select id as id, bank_name as bankName,bank_phone as bankPhone,bank_address as bankAddress from bank_t";
		PreparedStatement ps = connection.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			BankEntity bankEntity=new BankEntity();
			
			bankEntity.setId(rs.getInt("id"));
			bankEntity.setBankName(rs.getString("bankName"));
			bankEntity.setBankPhone(rs.getString("bankPhone"));
			bankEntity.setBankAddress(rs.getString("bankAddress"));
			
			bankLists.add(bankEntity);
		}
		
		return bankLists;
	}

	@Override
	public BankEntity findBankById(int id) throws Exception{
		Connection connection = JDBCUtil.getConnection();
		String sql="select id as id, bank_name as bankName,bank_phone as bankPhone,bank_address as bankAddress from bank_t where id=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		BankEntity bankEntity =new BankEntity();
		while(rs.next()){
			bankEntity.setId(rs.getInt("id"));
			bankEntity.setBankName(rs.getString("bankName"));
			bankEntity.setBankPhone(rs.getString("bankPhone"));
			bankEntity.setBankAddress(rs.getString("bankAddress"));
		}
		return bankEntity;
	}

	@Override
	public int updateBank(BankEntity entity) throws Exception{
		Connection connection = JDBCUtil.getConnection();
		String sql="update bank_t set bank_name=?,bank_phone=?,bank_address=? where id=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setString(1, entity.getBankName());
		ps.setString(2, entity.getBankPhone());
		ps.setString(3, entity.getBankAddress());
		ps.setInt(4, entity.getId());
		int counter = ps.executeUpdate();
		return counter;
	}

	@Override
	public int deleteBank(int id) throws Exception{
		Connection connection = JDBCUtil.getConnection();
		String sql="delete from bank_t where id=?";
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.setInt(1, id);
		int counter = ps.executeUpdate();
		return counter;
	}

}
